#include "stdafx.h"
#include "MyActiveObjectContainer.h"
#include "TypeIds.h"
#include "DefaultConfig.h"

namespace CO { namespace MyGroupNamespace { namespace MyPackageNamespace {

MyActiveObjectContainer::MyActiveObjectContainer(void) :
	//
	// Initialize Base class.
	//
	ActiveObjectContainer(PORT_COUNT),
	//
	// TODO:  Initialize attributes.
	//
	bindings_(),
	myActiveObject_(),
	myActiveHSM_(),
	myHsmMessageHandler_(),
	myConfigValue_(0)	// TODO:  Initialize attributes to default!
{
}

MyActiveObjectContainer::~MyActiveObjectContainer(void)
{
	this->destruct();
}

void MyActiveObjectContainer::destruct(void)
{
	//
	// Stop this object here.  It can't be done from base class!
	//
	this->stop();

	//
	// Un-bind children.
	//
	bindings_.clear();

	//
	// Clear child list to allow destruction of this class attributes here.
	//
	this->clearChildList();

	//
	// TODO:  Destruct children.
	//
	myActiveObject_.reset();
	myActiveHSM_.reset();
	myHsmMessageHandler_.reset();
}

bool MyActiveObjectContainer::initialize(SpiFoundation::Configuration::ConfigKey& configRoot)
{
	try	{
		//
		// TODO:  Initialize attributes with configuration settings.  Get key and value
		//		  will create if they don't exist, so be sure everyone uses the same defines
		//		  to avoid spelling errors!
		//
		configRoot.getValue_T(CFG_VAL_PATH_MyIntValue, myConfigValue_);

		//
		// TODO:  Create children based on config flag, call initialize,
		//			and add them to child list for base class operations.
		//
		if (myConfigValue_)
		{
			myActiveObject_ = MyActiveInterface::Create(MyActiveInterface::MY_ACTIVE_OBJECT);
			if (!myActiveObject_->initialize(configRoot))
			{
				throw 0; // To be caught below.
			}
			this->addChild(myActiveObject_);
		}
		if (myConfigValue_)
		{
			myActiveHSM_ = MyActiveInterface::Create(MyActiveInterface::MY_ACTIVE_HSM);
			if (!myActiveHSM_->initialize(configRoot))
			{
				throw 0; // To be caught below.
			}
			this->addChild(myActiveHSM_);
		}
		if (myConfigValue_)
		{
			myHsmMessageHandler_ = MyActiveInterface::Create(MyActiveInterface::MY_HSM_MESSAGE_HANDLER);
			if (!myHsmMessageHandler_->initialize(configRoot))
			{
				throw 0; // To be caught below.
			}
			this->addChild(myHsmMessageHandler_);
		}

		//
		// TODO:  Bind children.  Use inner wall to bind children to outside.
		//
		if (myConfigValue_)
		{
			bindings_.push_back(
				CO::SpiFoundation::RoomFW::BindingSP(
					new CO::SpiFoundation::RoomFW::ActiveObjectsBinding(
						this->getInnerWall(), PORT_MY_PORT_1,
						myActiveObject_, MyActiveInterface::PORT_MY_PORT_1)));
			bindings_.push_back(
				CO::SpiFoundation::RoomFW::BindingSP(
					new CO::SpiFoundation::RoomFW::ActiveObjectsBinding(
						this->getInnerWall(), PORT_MY_PORT_3,
						myActiveObject_, MyActiveInterface::PORT_MY_PORT_3)));
			bindings_.push_back(
				CO::SpiFoundation::RoomFW::BindingSP(
					new CO::SpiFoundation::RoomFW::ActiveObjectsBinding(
						myActiveObject_, MyActiveInterface::PORT_MY_PORT_2,
						myActiveHSM_, MyActiveInterface::PORT_MY_PORT_1)));
			bindings_.push_back(
				CO::SpiFoundation::RoomFW::BindingSP(
					new CO::SpiFoundation::RoomFW::ActiveObjectsBinding(
						myActiveObject_, MyActiveInterface::PORT_MY_PORT_2,
						myHsmMessageHandler_, MyActiveInterface::PORT_MY_PORT_1)));
		}
	}
	catch (...)
	{
		this->destruct();
		return false;
	}
	return true;
}

SpiFoundation::RoomFW::PortIndex::PrimaryIndex MyActiveObjectContainer::getSystemPort(void) const
{
	return SpiFoundation::RoomFW::PortIndex::INVALID;
}

void MyActiveObjectContainer::addChildren(SpiFoundation::Interface::ISystemComponentSPList& childList) const
{
	//
	// TODO:  Call addChildren on each child container.
	//
	// myChildContainer->addChildren(childList);

	//
	// TODO:  Add children to system list.
	//
	childList.push_back(myActiveObject_);
	childList.push_back(myActiveHSM_);
	childList.push_back(myHsmMessageHandler_);
}

SpiFoundation::RoomFW::ObjectId MyActiveObjectContainer::getObjectId(void) const
{
	//
	// TODO:  Return a unique id for your object.
	//
	return OID_MY_ACTIVE_OBJECT_CONTAINER_ID;
}

const char* MyActiveObjectContainer::getObjectName(void) const
{
	//
	// TODO:  Add text for your object name for debugging.
	//
	return "MyActiveObjectContainer";
}

}}}
